package com.jiahe.video.runner;


import com.jiahe.video.config.AppConfig;
import com.jiahe.video.config.StepConfig;
import com.jiahe.video.model.Camera;
import com.jiahe.video.service.ApiService;
import com.jiahe.video.service.AppService;
import com.jiahe.video.service.impl.AbsCommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @author author
 */
@Slf4j
@Component
@Order(value=1)
public class AppRunner extends AbsCommonService implements CommandLineRunner {

    @Resource
    private ApiService apiService;
    @Resource
    private AppService appService;
    @Resource
    private ThreadPoolTaskScheduler scheduler;
    @Autowired
    private AppConfig appConfig;

    @Override
    public void run(String... args) throws Exception {

        String OS = System.getProperty("os.name").toLowerCase();
        if (OS.contains("win")) {
            //System.load(ClassLoader.getSystemResource("lib/opencv_videoio_ffmpeg470_64.dll").getPath());
            String dllPath = appConfig.getDllPath();
            System.load(dllPath);
        }


        // 初始化用户昵称
        apiService.nicknameInit();

        sendWeComMessage("-- 服务重启 --");
        List<Camera> cameraList = apiService.getCameraList();
        log.info("共获取到{}个摄像头",cameraList.size());

        // 低频
        CompletableFuture.runAsync(() -> {
            while(true){
                for (Camera camera : cameraList) {
                    if (camera.getVision() != 2 ) {
                        try{
                            appService.detectionOnce(camera);
                        }catch (Exception e){
                            log.error(e.toString());
                        }
                    }
                }
            }
        });


        // 高频
        for (Camera camera : cameraList) {
            if (camera.getVision()==2) {
                Thread.sleep(2000);
                appService.buildWorker(camera);
            }
        }
    }

}
